<?php
require_once $_SERVER[DOCUMENT_ROOT].'/clientes/src/lib/db/ConnectionDB.php';

class Contacto{
	
	//----------------------------------------------------------------------------
	//Delimitador de la tabla
	//----------------------------------------------------------------------------
	
	/**
	 * El constrains de la clase
	 */
	private static $table_nombre = "contacto";
	private static $constrains = array(	array("id",				ConnectionDB::INT,	ConnectionDB::NOT_NULL, ConnectionDB::PRIMARY_KEY,	ConnectionDB::NO_DEFAULT_VALUE),	//El id
										array("id_contacto1",	ConnectionDB::INT,	ConnectionDB::NOT_NULL,	ConnectionDB::INDEX_KEY,	ConnectionDB::NO_DEFAULT_VALUE),	//El id del contacto_1
										array("id_contacto2",	ConnectionDB::INT,	ConnectionDB::NOT_NULL,	ConnectionDB::INDEX_KEY,	ConnectionDB::NO_DEFAULT_VALUE));	//El id del contacto_2

	//----------------------------------------------------------------------------
	//Atributos
	//----------------------------------------------------------------------------								
	
	/**
	 * Id de la relaci�n
	 */
	private $id;
		
	/**
	 * Id del contacto 1 de la relaci�n
	 */
	private $id_contacto_1;
	
	/**
	 * Id del contacto 2 de la relaci�n
	 */
	private $id_contacto_2;
	
	/**
	 * V�nculo con la Tabla en la Base de Datos
	 */	
	private $tabla;
	
	//----------------------------------------------------------------------------
	//M�todo constructor
	//----------------------------------------------------------------------------
	
	public function __construct($id = 0, $id_contacto_1 = 0, $id_contacto_2 = 0){
		$this->id = $id;
		$this->id_contacto_1 = $id_contacto_1;
		$this->id_contacto_2 = $id_contacto_2;
		
		//Realiza lo debido a la base de datos
		$connection = new ConnectionDB($_SESSION['USER_NAME'],$_SESSION['USER_PASS']);
		$this->tabla = $connection->get_table(Contacto::$table_nombre, Contacto::$constrains);
	}
	
	//----------------------------------------------------------------------------
	//Getters y Setters B�sicos de la Clase
	//----------------------------------------------------------------------------

	/**
	 * Retorna el Id de la instancia
	 */
	public function get_id(){
		return $this->id;
	}
		
	/**
	 *Retorna el id del Contacto 1
	 */
	public function get_id_contacto_1(){
		return $this->id_contacto_1;
	}

	/**
	 *Retorna el id del Contacto 2
	 */
	public function get_id_contacto_2(){
		return $this->id_contacto_2;
	}
		
	//----------------------------------------------------------------------------
	//Busqueda en la Base de Datos
	//----------------------------------------------------------------------------

    public static function search_by_id_contacto_list($id_contacto){
		$contacto = new Contacto();

		$params = array(array("id_contacto1", $id_contacto));
		$response_id = 	$contacto->tabla->get_value_by_field("id", $params);

		$answer = array();

		foreach($response_id as $i => $value){
			$user = new Contacto($response_id[$i]);
			$user->update_from_database_by_id();

			$answer[] = $user;
		}

        $params = array(array("id_contacto2", $id_contacto));
		$response_id = 	$contacto->tabla->get_value_by_field("id", $params);

		foreach($response_id as $i => $value){
			$user = new Contacto($response_id[$i]);
			$user->update_from_database_by_id();

			$answer[] = $user;
		}
		return $answer;
	}

	public static function search_by_id_contacto($id_contacto_1,$id_contacto_2){
		$contacto = new Contacto();
		
		$params = array(array("id_contacto1", $id_contacto_1),
						array("id_contacto2", $id_contacto_2));
		$response_id = 	$contacto->tabla->get_value_by_field("id", $params);
		
		$answer = array();
		
		foreach($response_id as $i => $value){
			$user = new Contacto($response_id[$i]);
			$user->update_from_database_by_id();
			
			$answer[] = $user;
		}

        $params = array(array("id_contacto1", $id_contacto_2),
						array("id_contacto2", $id_contacto_1));
		$response_id = 	$contacto->tabla->get_value_by_field("id", $params);

		foreach($response_id as $i => $value){
			$user = new Contacto($response_id[$i]);
			$user->update_from_database_by_id();

			$answer[] = $user;
		}
		return $answer;
	}
		
	//----------------------------------------------------------------------------
	//Actualizaci�n con la Base de Datos
	//----------------------------------------------------------------------------
	
	/**
	 * Actualiza la informaci�n en la Base de Datos
	 */
	public function update_database(){
		//Revisa si existe y lo agrega
		$params = array(array("id_contacto1",$this->id_contacto_1),
						array("id_contacto2",$this->id_contacto_2));
		$this->tabla->update($params);
		$this->id = $this->tabla->get_id();
	}
	
	/**
	 * Actualiza la informaci�n desde la Base de Datos seg�n el id
	 */
	public function update_from_database_by_id(){
		$params = array(array("id",$this->id));
		
		//Toma los valores
		$id_contacto_1 = 	$this->tabla->get_value_by_field("id_contacto1",	$params);
		$id_contacto_2 =	$this->tabla->get_value_by_field("id_contacto2",	$params);
		
		$this->id_contacto_1 = $id_contacto_1[0];
		$this->id_contacto_2 = $id_contacto_2[0];		
	}
}
?>